package a;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class TypingTutor extends JFrame {
    private JTextArea textArea;
    private JLabel timeLabel, speedLabel;
    private JButton startButton, stopButton;
    private Timer timer, typingTimer;
    private long startTime;
    private int typedChars, totalChars;

    public TypingTutor() {
        super("打字通");

        // 创建文本框和计时器标签
        textArea = new JTextArea(10, 30);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        timeLabel = new JLabel("用时：0秒");
        speedLabel = new JLabel("速度：0字/分");

        // 创建开始和结束按钮
        startButton = new JButton("开始");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startTyping();
            }
        });
        stopButton = new JButton("结束");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                stopTyping();
            }
        });
        stopButton.setEnabled(false);

        // 创建布局并添加组件
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.add(scrollPane, BorderLayout.CENTER);
        panel1.add(new JLabel("练习文章："), BorderLayout.NORTH);
        JPanel panel2 = new JPanel();
        panel2.add(startButton);
        panel2.add(stopButton);
        panel2.add(timeLabel);
        panel2.add(speedLabel);
        JPanel panel3 = new JPanel(new BorderLayout());
        panel3.add(panel1, BorderLayout.CENTER);
        panel3.add(panel2, BorderLayout.SOUTH);
        add(panel3);

        // 设置窗口大小和默认关闭操作
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    // 从文件中读取文章
    private String getArticle() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("article.txt"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        reader.close();
        return sb.toString();
    }

    // 开始打字
    private void startTyping() {
        try {
            // 读取文章并显示在文本框中
            String article = getArticle();
            textArea.setText(article);

            // 开启计时器
            startTime = System.currentTimeMillis();
            timer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    long elapsedTime = System.currentTimeMillis() - startTime;
                    timeLabel.setText("用时：" + (elapsedTime / 1000) + "秒");
                }
            });
            timer.start();

            // 开启打字计时器
            typedChars = 0;
            totalChars = article.length();
            typingTimer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    typedChars = textArea.getText().length();
                    int speed = (int)(60.0 * typedChars / totalChars);
                    speedLabel.setText("速度：" + speed + "字/分");
                }
            });
            typingTimer.start();

            // 设置按钮状态
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            textArea.setEditable(true);
            textArea.requestFocusInWindow();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "无法读取文章文件！");
        }
    }

    // 停止打字
    private void stopTyping() {
        // 停止计时器
        timer.stop();
        typingTimer.stop();

        // 计算打字成绩并显示
        long elapsedTime = System.currentTimeMillis() - startTime;
        int speed = (int)(60.0 * typedChars / totalChars);
        JOptionPane.showMessageDialog(null, "用时：" + (elapsedTime / 1000) + "秒，速度：" + speed + "字/分");

        // 设置按钮状态
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        textArea.setEditable(false);
    }

    public static void main(String[] args) {
        new TypingTutor();
    }
}
